package com.amkai.mobile.client.appointmentdetails;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.amkai.mobile.bean.registration.MobilePatient;
import com.amkai.mobile.bean.scheduler.appointment.MobileAppointmentDetails;
import com.amkai.mobile.client.R;
import com.amkai.mobile.common.job.scheduler.MobileSchedulerJob;
import com.amkai.mobile.common.job.scheduler.MobileSchedulerJobResult;
import com.amkai.mobile.utils.manager.job.MobileServiceManager;

/**
 * 
 * @author lkovari
 *
 */
public class PatientTab extends Activity {
	private TextView patientNameTextView = null;
	private TextView dobTextView = null;
	private TextView ssnTextView = null;
	private TextView genderTextView = null;
	private TextView accntTextView = null;
	private TextView addr1TextView = null;
	private TextView cityCtyZipTextView = null;
	private TextView homePhoneTextView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.patienttab);

        // capture ids
        Bundle bundle = getIntent().getExtras();
        Long beId = bundle.getLong("department");
        Long passId = bundle.getLong("passport");
        Long apptId = bundle.getLong("appointment_id");
        
        // download appointmentTetails
		try {
//			MobileSchedulerJobResult schedulerResult = MobileServiceManager.processMobileJob(PatientTab.this, MobileSchedulerJob.appointmentDetailsById(apptId));
			MobileSchedulerJobResult schedulerResult = MobileServiceManager.processMobileJob(PatientTab.this, MobileSchedulerJob.patientDetailsById(apptId));
			if (schedulerResult.getException() == null) {
//				MobileAppointmentDetails mobileAppointmentDetails = schedulerResult.getMobileAppointmentDetails();
//				showPatientDetails(mobileAppointmentDetails);
				MobilePatient mobilePatient = schedulerResult.getMobilePatient();
				showPatientDetails(mobilePatient);
			}
			else {
				Toast msg = Toast.makeText(PatientTab.this, "Download patient data failed!", Toast.LENGTH_SHORT);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void showPatientDetails(MobileAppointmentDetails mobileAppointment) {
        patientNameTextView = (TextView) findViewById(R.id.patnameValueTextView);
        patientNameTextView.setText(mobileAppointment.mobilePatient.lastName + " " + mobileAppointment.mobilePatient.firstName); 
        
        dobTextView = (TextView) findViewById(R.id.patdobValueTextView);
		dobTextView.setText(mobileAppointment.mobilePatient.dob + " (" + mobileAppointment.mobilePatient.age + ")");

        ssnTextView = (TextView) findViewById(R.id.ssnValueTextView);
		ssnTextView.setText(mobileAppointment.mobilePatient.ssn);
		
		genderTextView = (TextView) findViewById(R.id.patGenderValueTextView);
		genderTextView.setText(mobileAppointment.mobilePatient.gender);
		
		accntTextView = (TextView) findViewById(R.id.accntNumValueTextView);
		if (mobileAppointment.mobileAccount != null)
			accntTextView.setText(mobileAppointment.mobileAccount.number);
		
		if (mobileAppointment.mobilePatient.homeAddress != null) {
			addr1TextView = (TextView) findViewById(R.id.addrValueTextView);
			addr1TextView.setText(mobileAppointment.mobilePatient.homeAddress.address1);

			cityCtyZipTextView = (TextView) findViewById(R.id.cityValueTextView);
			String cityCtyZip =  mobileAppointment.mobilePatient.homeAddress.city + " " + mobileAppointment.mobilePatient.homeAddress.country + " " + mobileAppointment.mobilePatient.homeAddress.zip;
			cityCtyZipTextView.setText(cityCtyZip);

			homePhoneTextView = (TextView) findViewById(R.id.phoneValueTextView);
			if (mobileAppointment.mobilePatient.homeAddress.phone != null) {
				String homePhone = mobileAppointment.mobilePatient.homeAddress.phone.number + " " + mobileAppointment.mobilePatient.homeAddress.phone.ext;
				homePhoneTextView.setText(homePhone);
			}
		}
		
	}

	/**
	 * 
	 * @param mobilePatient
	 */
	private void showPatientDetails(MobilePatient mobilePatient) {
        patientNameTextView = (TextView) findViewById(R.id.patnameValueTextView);
        patientNameTextView.setText(mobilePatient.lastName + " " + mobilePatient.firstName); 
        
        dobTextView = (TextView) findViewById(R.id.patdobValueTextView);
		dobTextView.setText(mobilePatient.dob + " (" + mobilePatient.age + ")");

        ssnTextView = (TextView) findViewById(R.id.ssnValueTextView);
		ssnTextView.setText(mobilePatient.ssn);
		
		genderTextView = (TextView) findViewById(R.id.patGenderValueTextView);
		genderTextView.setText(mobilePatient.gender);
		
		accntTextView = (TextView) findViewById(R.id.accntNumValueTextView);
		if (mobilePatient.mobileAccount != null)
			accntTextView.setText(mobilePatient.mobileAccount.number);
		
		if (mobilePatient.homeAddress != null) {
			addr1TextView = (TextView) findViewById(R.id.addrValueTextView);
			addr1TextView.setText(mobilePatient.homeAddress.address1);

			cityCtyZipTextView = (TextView) findViewById(R.id.cityValueTextView);
			String cityCtyZip =  mobilePatient.homeAddress.city + " " + mobilePatient.homeAddress.country + " " + mobilePatient.homeAddress.zip;
			cityCtyZipTextView.setText(cityCtyZip);

			homePhoneTextView = (TextView) findViewById(R.id.phoneValueTextView);
			if (mobilePatient.homeAddress.phone != null) {
				String homePhone = mobilePatient.homeAddress.phone.number + " " + mobilePatient.homeAddress.phone.ext;
				homePhoneTextView.setText(homePhone);
			}
		}
	}
	
}
